/**
 * Created by wang on 2015-10-26.
 */
define(function () {
    'use strict';

    function ctrl($scope,$timeout,$stateParams,$ionicScrollDelegate,$sce,$filter,InfoService,CommonService) {
        var par = $scope.param = new Array();

        $scope.$on('$ionicView.beforeEnter', function() {
            par.theme_mark =$stateParams['id'];
            init();
        });

        $scope.doRefresh = function() {
            init();
            $scope.$broadcast("scroll.refreshComplete");
        };

        $scope.expand = function(){
            $timeout(function(){
                $ionicScrollDelegate.resize();
                $ionicScrollDelegate.scrollBottom(true);
            }, 100);
        };

        function init(){
            InfoService.getThemeDetail(par.theme_mark).then(function(data){
                if(data&&data.relate_type){
                    if(data.theme_desc){
                        data.theme_desc = $sce.trustAsHtml(data.theme_desc);
                    }
                    $scope.info = data;
                    InfoService.getOtcProductsByChannelId($scope.info.xcm_type).then(
                        function(data) {
                            if(data && data.length > 0){
                                for(var i=0;i<data.length;i++){
                                    var fund = data[i];
                                    fund.prod_min_subscribe = $filter('prodMinSubscribe')(fund.prod_min_subscribe);
                                    if("0" == fund.otc_status){
                                        var days = CommonService.stringToDate(fund.ipo_begin_date).getTime() -new Date().getTime();
                                        fund.begin_date_show = Math.round(days / (1000 * 60 * 60 * 24));
                                    }
                                    fund.sellStatus = CommonService.getOtcSellStatus(fund.prod_status, fund.otc_status);
                                    fund.ipo_begin_date_show = CommonService.stringToDate(fund.ipo_begin_date);
                                    fund.prod_name_short = (fund.prod_name && fund.prod_name.indexOf("(") != -1) ? fund.prod_name.split("(")[0] :fund.prod_name;
                                }
                            }
                            $scope.funds = data;
                        }
                    )
                }
            });
        }
    }

    ctrl.$inject = ['$scope','$timeout','$stateParams','$ionicScrollDelegate','$sce','$filter','InfoService','CommonService'];
    return ctrl;
});